#pragma once

#include "../poco/community-post.hh"
#include "../repository/community-repository.hh"

namespace WordMemory
{
    class CommunityService
    {
    public:
        static CommunityService& instance()
        {
            static CommunityService inst;
            return inst;
        }

        std::vector<CommunityPost> getCommunityPosts(int current_user_id)
        {
            return CommunityRepository::instance().getCommunityPosts(current_user_id);
        }

        CommunityPost getPostById(int post_id, int current_user_id = 0)
        {
            CommunityPost post = CommunityRepository::instance().getPostById(post_id);
            if (current_user_id > 0)
            {
                post.is_liked_by_current_user = CommunityRepository::instance().isPostLikedByUser(post_id, current_user_id);
            }
            return post;
        }

        bool toggleLikePost(int post_id, int user_id)
        {
            if (user_id <= 0)
                throw ServiceExcept("User must be logged in to like a post.");
            return CommunityRepository::instance().likePost(post_id, user_id);
        }

        void incrementShareCount(int post_id)
        {
            CommunityRepository::instance().incrementShareCount(post_id);
        }

    private:
        CommunityService() = default;
        CommunityService(const CommunityService&) = delete;
        CommunityService& operator=(const CommunityService&) = delete;
    };
}